package com.xli.mis.codeitems.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xli.dto.component.TreeNode;
import com.xli.dto.component.model.TableModel;
import com.xli.dto.component.model.TreeModel;
import com.xli.dto.controller.BaseController;
import com.xli.dto.params.IdsDTO;
import com.xli.dto.result.ResultVO;
import com.xli.dto.result.status.Status;
import com.xli.dto.validation.group.IGroup;
import com.xli.mis.codeitems.entity.CodeItems;
import com.xli.mis.codeitems.entity.dto.CodeItemsAddDTO;
import com.xli.mis.codeitems.entity.dto.CodeItemsDTO;
import com.xli.mis.codeitems.entity.dto.CodeItemsSearchDTO;
import com.xli.mis.codeitems.entity.dto.CodeItemsUpdateDTO;
import com.xli.mis.codeitems.entity.mapper.ICodeItemsMapper;
import com.xli.mis.codeitems.entity.vo.CodeItemsVO;
import com.xli.mis.codeitems.service.ICodeItemsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "字典项")
@RestController
@RequestMapping("/codeItems")
public class CodeItemsController extends BaseController {

    @Autowired
    ICodeItemsService iCodeItemsService;

    @Operation(summary = "保存字典项", description = "保存字典子项")
    @PostMapping(value = "/add")
    public ResultVO<String> add(@RequestBody @Validated(IGroup.add.class) CodeItemsAddDTO dto) {
        boolean isExists = iCodeItemsService.isExists(Long.valueOf(dto.getCodeId()), dto.getItemValue());
        if (!isExists) {
            CodeItems codeItems = ICodeItemsMapper.INSTANCE.toEntity(dto);
            codeItems.setId(IdUtil.getSnowflakeNextId());
            if (iCodeItemsService.insert(codeItems)) {
                return new ResultVO<>(Status.SUCCESS, "保存成功");
            }
            return new ResultVO<>(Status.FAILED, "保存失败");
        }
        return new ResultVO<>(Status.FAILED, "保存失败，字典项已存在");
    }

    @Operation(summary = "删除字典项", description = "删除字典子项")
    @PostMapping(value = "/delete")
    public ResultVO<String> delete(@RequestBody @Validated(IGroup.delete.class) IdsDTO dto) {
        int i = 0;
        for (String id : dto.getIds()) {
            if (iCodeItemsService.delete(Long.valueOf(id))) {
                i++;
            }
        }
        return new ResultVO<>(Status.SUCCESS, i + "条数据删除成功");
    }

    @Operation(summary = "更新字典项", description = "更新字典子项")
    @PostMapping(value = "/update")
    public ResultVO<String> update(@RequestBody @Validated(IGroup.update.class) CodeItemsUpdateDTO dto) {
        boolean isExists = iCodeItemsService.isExists(Long.valueOf(dto.getCodeId()), dto.getItemValue());
        if (isExists) {
            if (iCodeItemsService.update(ICodeItemsMapper.INSTANCE.toEntity(dto))) {
                return new ResultVO<>(Status.SUCCESS, "更新成功");
            }
            return new ResultVO<>(Status.FAILED, "更新失败");
        }
        return new ResultVO<>(Status.FAILED, "更新失败，字典项已存在");
    }

    @Operation(summary = "查询字典项列表", description = "查询字典子项列表")
    @PostMapping(value = "/search")
    public ResultVO<TableModel<CodeItemsVO, CodeItems, CodeItemsSearchDTO>> search(@RequestBody @Validated(IGroup.search.class) CodeItemsSearchDTO dto) {

        TableModel<CodeItemsVO, CodeItems, CodeItemsSearchDTO> tableModel = new TableModel<>(dto) {
            @Override
            public List<CodeItemsVO> fetch(int current, int pageSize) {
                Page<CodeItems> page = iCodeItemsService.findList(getQueryWrapper(), current, pageSize);
                this.setTotal(page.getTotal());
                return page.convert(ICodeItemsMapper.INSTANCE::toVO).getRecords();
            }
        };
        return new ResultVO<>(Status.SUCCESS, "查询成功", tableModel);
    }

    @Operation(summary = "查询字典树", description = "查询字典树")
    @PostMapping(value = "/fetchTree")
    public ResultVO<TreeModel<CodeItemsVO>> fetchTree(@RequestBody @Validated(IGroup.detail.class) CodeItemsDTO dto) {

        TreeModel<CodeItemsVO> treeModel = new TreeModel<>() {
            @Override
            public List<TreeNode<CodeItemsVO>> fetch() {
                List<TreeNode<CodeItemsVO>> treeNodeList = new ArrayList<>();
                List<CodeItems> codeItemsList = iCodeItemsService.findItemListByCodeId(Long.valueOf(dto.getCodeId()));
                for (CodeItems codeItems : codeItemsList) {
                    TreeNode<CodeItemsVO> childNode = new TreeNode<>();
                    childNode.setId(codeItems.getId());
                    childNode.setText(codeItems.getItem_text());
                    childNode.setPid(codeItems.getPid());
                    treeNodeList.add(childNode);
                }
                return treeNodeList;
            }
        };
        return new ResultVO<>(Status.SUCCESS, "查询成功", treeModel);
    }
}
